package com.wxmp.wxapi.service.impl;

import com.wxmp.core.util.AjaxResult;
import com.wxmp.core.util.DateUtil;
import com.wxmp.wxapi.domain.WxpayOrder;
import com.wxmp.wxapi.domain.WxpayUnionAccount;
import com.wxmp.wxapi.mapper.WxpayOrderMapper;
import com.wxmp.wxapi.mapper.WxpayUnionAccountMapper;
import com.wxmp.wxapi.service.UnionCardAccountService;
import com.wxmp.wxapi.service.WxRechargeService;
import com.wxmp.wxapi.vo.PayRecord;
import com.wxmp.wxapi.vo.RechargeOrder;
import com.wxmp.wxapi.vo.UnionCardAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Johnkee
 */
@Service
public class WxRechargeServiceImpl implements WxRechargeService {

    private static Logger log = LoggerFactory.getLogger(WxRechargeServiceImpl.class);

    @Autowired
    private WxpayUnionAccountMapper wxpayUnionAccountMapper;

    @Autowired
    private WxpayOrderMapper wxpayOrderMapper;

    @Autowired
    private UnionCardAccountService unionCardAccountService;

    @Override
    public UnionCardAccount getCardAccount(String openid) {
        log.info("query union card account by openid [{}]", openid);
        WxpayUnionAccount wxpayUnionAccount = wxpayUnionAccountMapper.selectByOpenId(openid);
        if(wxpayUnionAccount != null){
            UnionCardAccount cardAccount = new UnionCardAccount();
            cardAccount.setOpenid(openid);
            cardAccount.setCardNo(wxpayUnionAccount.getCardNo());
            cardAccount.setCardOwner(wxpayUnionAccount.getCardOwner());
            cardAccount.setUsableAmount(unionCardAccountService.getUsableAmountByCardNo(wxpayUnionAccount.getCardNo()));
            return cardAccount;
        }else {
            return null;
        }
    }

    @Override
    public AjaxResult bindCard(String openid, String cardNo, String cardOwner) {
        WxpayUnionAccount wxpayUnionAccount = wxpayUnionAccountMapper.selectByOpenId(openid);
        if(wxpayUnionAccount != null){
            return AjaxResult.failure("当前微信已经绑定了卡号为："+ wxpayUnionAccount.getCardNo() +"的卡，请解绑后再重试");
        }else {
            wxpayUnionAccount = new WxpayUnionAccount();
            wxpayUnionAccount.setCardNo(cardNo);
            wxpayUnionAccount.setCardOwner(cardOwner);
            wxpayUnionAccount.setOpenId(openid);
            wxpayUnionAccount.setFromDate(new Date());
            wxpayUnionAccount.setThruDate(DateUtil.changeStrToDate2("2029-12-31"));
            wxpayUnionAccount.setRemark("用户自行在微信公共号里绑定一卡通");
            int insertCount = wxpayUnionAccountMapper.insertSelective(wxpayUnionAccount);
            if(insertCount == 1) {
                return AjaxResult.saveSuccess();
            }
        }
        return AjaxResult.saveSuccess();
    }

    @Override
    public List<RechargeOrder> gerRechargeListByPage(RechargeOrder rechargeOrder) {
        List<WxpayOrder> wxpayOrderList = wxpayOrderMapper.getOrderListByPage(rechargeOrder);
        List<RechargeOrder> rechargeOrderList = new ArrayList<>();
        wxpayOrderList.forEach(wxpayOrder -> {
            RechargeOrder rechargeOrderEntry = new RechargeOrder();
            rechargeOrderEntry.setOpenid(wxpayOrder.getOpenId());
            rechargeOrderEntry.setRechargeTime(wxpayOrder.getCreateDate());
            rechargeOrderEntry.setRechargeStatus(wxpayOrder.getStatus() > 1?"充值成功":"充值未成功");
            rechargeOrderEntry.setChannelName("微信公众号");
            rechargeOrderEntry.setAmount(wxpayOrder.getAmount()/100.00f);
            rechargeOrderList.add(rechargeOrderEntry);
        });
        return rechargeOrderList;
    }

    @Override
    public List<PayRecord> getPayListByPage(PayRecord payRecord) {
        WxpayUnionAccount wxpayUnionAccount = wxpayUnionAccountMapper.selectByOpenId(payRecord.getOpenid());
        payRecord.setCardNo(wxpayUnionAccount.getCardNo());
        List<PayRecord> recordList = unionCardAccountService.getPayListByPage(payRecord);
        return recordList;
    }

    @Override
    public String getCardOwnerByCardNo(String cardNo) {
        return unionCardAccountService.getCardOwnerByCardNo(cardNo);
    }

    @Override
    public int saveWxPayOrder(WxpayOrder wxpayOrder) {
        return wxpayOrderMapper.insertSelective(wxpayOrder);
    }

    @Override
    public int updatePayOrderStatus(WxpayOrder wxpayOrder) {
        return wxpayOrderMapper.updateByPrimaryKeySelective(wxpayOrder);
    }

    @Override
    public WxpayOrder getPayOrderByPrimaryKey(Long aLong) {
        return wxpayOrderMapper.selectByPrimaryKey(aLong);
    }
}
